import fs from 'fs';
import path from 'path';

//获取数据;
let TemplateList= async (ctx) => {
    let res = await ctx.db.querys(`select * from template`); 
    ctx.body = {code:1,msg:'获取模板数据成功',data:[...res]}
}

//文件提交;
let TemplateAdd=async (ctx)=>{
    
    let {title,type,downurl,userid} = ctx.request.body;
    let data=await ctx.db.querys(`select * from template`);
    let dataIndex=data.findIndex(item=>item.title===title);
    if(dataIndex!==-1) return ctx.body={code:0,msg:'文件名不能重复，请重新输入！'}
    downurl='/excel/'+downurl;
    let createtime = new Date().getTime()+'';
    let res = await ctx.db.insert('template',{title,type,downurl,userid,createtime});
    if(res.affectedRows === 0)  return ctx.body = {code:0,msg:'提交失败，请重新提交'};
    ctx.body = {code:1,msg:'提交成功'}
}

//文件上传;
let TemplateUploadFiles =async(ctx)=>{
    let isOpr = await ctx.isJudge(ctx,'U');

    // 判断当前用户是否可以操作
    if(!isOpr){
        ctx.body = {code:0,msg:'您无权操作'}
        return
    }
    let sureUploadArr = ['xls','xlsx','xlsb','xlsm','xlst','doc','docx','dotx','pptx','pdf'];
    //获取文件；
    // console.log(ctx.request.files)
    let user = Object.keys(ctx.request.files)[0];
    let file = ctx.request.files[user];
    //处理；
    // console.log(ctx.request)
    // console.log(file);
    if(sureUploadArr.some(item=>item===file.name.split('.').pop())){
        //读取；
        let rStream = fs.createReadStream(file.path);
        //输出；
        let wStream = fs.createWriteStream(path.join(__dirname,'../../public/excel/'+file.name));
        //管道流：
        rStream.pipe(wStream)
        ctx.body={code:1,msg:'文件上传成功',data:'/excel/'+file.name}
        return 
    }
    ctx.body={code:0,msg:'文件格式不正确，请确认后在上传'}
}

//模板搜索;
let TemplateSearch=async ctx=>{
    const {title,type}=ctx.request.body;
    let data=await ctx.db.querys(`select * from template`);
    data=data.filter(item=>item.title.includes(title));
    if(type===''||type===undefined) return ctx.body={code:1,msg:'返回模板',data:data};
    data=data.filter(item=>item.type===type);
    if(!data.length) return ctx.body={code:0,msg:'暂无数据',data:[]};
    ctx.body={code:1,msg:'搜索成功',data:data};
}

//录屏管理;
let showTemplateBaidu = async (ctx,next) => {
    let arr = await ctx.db.querys(`select * from screen`);
    let classlist =await ctx.db.querys(`select * from class`);
    arr.forEach(item=>{
        classlist.forEach(jtem=>{
            if(item.classid===jtem.classid){
                item.classid = jtem.classname
            }
        })
    })
    ctx.body = {code:1,msg:'数据获取成功',data:{arr,classlist}}
}

//录屏上传;
let addTemplateBaidu = async (ctx,next) => {
    let isOpr = await ctx.isJudge(ctx,'U');

    // 判断当前用户是否可以操作
    if(!isOpr){
        ctx.body = {code:0,msg:'您无权操作'}
        return
    }
    let {name,filepath,filecode, userid,type,classid,remarks } = ctx.request.body;
    let obj = {name,filepath,filecode, userid,type,classid,remarks}
    let time = new Date(new Date().getTime()).setHours(0, 0, 0, 0)
    obj.createtime = time;
    let add = await ctx.db.insert('screen',obj)
    if(!add.affectedRows) return ctx.body = {code:0,msg:'添加失败，查看下数据是否正确'} 
    ctx.body = {code:1,msg:'添加成功'}
}

export default {
    TemplateList,TemplateAdd,TemplateUploadFiles,TemplateSearch,addTemplateBaidu,showTemplateBaidu
}